/******************************************************************************
 * 
 *****************************************************************************/

package com.gw.business.statistics.behavior.controller;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gw.business.analysis.model.UserTrackDataStatistics;
import com.gw.business.common.util.DateUtil;
import com.gw.business.statistics.behavior.model.StatiscChartModel;
import com.gw.business.statistics.behavior.model.UserChartModel;
import com.gw.platform.controller.DefaultPageController;
import com.gw.platform.util.DateUtils;
import com.gw.platform.util.JsonUtils;

/**
 * FIXME 类注释信息(此标记自动生成,注释填写完成后请删除)
 * 
 * <pre>
 * 
 * [调用关系:
 * 实现接口及父类:
 * 子类:
 * 内部类列表:
 * ]
 * </pre>
 * 
 * @author kidy
 * @since 1.0
 * @version 2015年2月5日 kidy
 */
@Controller
@RequestMapping("staticChart")
public class StatisticChartController extends DefaultPageController {
    
    @RequestMapping("/page")
    public String queryCharts() {
        return "system/right";
    }
    
    /**
     * 跳转到用户收益页面
     * 
     * @return
     */
    @RequestMapping(value = "/ajaxStatsicCharts")
    @ResponseBody
    public Object ajaxStatsicCharts(String type) {
        String josnResult;
        String time =DateUtil.toYyyymmdd(new Date());
        String todayTime =DateUtil.toYyyymmdd(DateUtil.getBeforeDay(new Date(), 1));
        Map<String, Object> map = new HashMap<String, Object>();
        if (time != null && time != "") {
            map.put("time", time);
        }
        Map<String, Object> map1 = new HashMap<String, Object>();
        map1.put("time", todayTime);
        
        String startTime =DateUtil.toYyyymmdd(DateUtil.getBeforeDay(new Date(),7));
        Map<String, Object> map2 = new HashMap<String, Object>();
        map2.put("startTime", startTime);
        map2.put("endTime", todayTime);
        List<Integer> visitLists = new ArrayList<Integer>();
        List<Integer> visitTodayLists = new ArrayList<Integer>();
        List<Object> visitAvgLists = new ArrayList<Object>();
        List<UserTrackDataStatistics> staticPointList = new ArrayList<UserTrackDataStatistics>();
        List<UserTrackDataStatistics> staticTodayPointList = new ArrayList<UserTrackDataStatistics>();
        List<UserChartModel> staticAvgPointList = new ArrayList<UserChartModel>();
        StatiscChartModel chartModel = new StatiscChartModel();
        List<UserTrackDataStatistics> staticList = (List<UserTrackDataStatistics>) defaultService.find("queryTrackDateChartStatistics", map);
        List<UserTrackDataStatistics> staticListToday = (List<UserTrackDataStatistics>) defaultService.find("queryTrackDateChartStatistics", map1);
        List<UserChartModel> staticListAvg = (List<UserChartModel>) defaultService.find("queryTrackAvgChartStatistics", map2);
        // 补点
        staticPointList = staticPoint(staticList);
        int hours = new Date().getHours();
        Date now = new Date();
        Long nowTime = now.getTime();
        if (nowTime % (1000 * 60 * 60) != 0) {
            hours = hours - 1;
        }
        //当前时间之后的数据，为空
        for (UserTrackDataStatistics userChartModel : staticPointList) {
            if (userChartModel.getHours() > hours) {
                userChartModel.setVisitCount(null);
                userChartModel.setAdvisoryCount(null);
                userChartModel.setDemoCount(null);
                userChartModel.setRealCount(null);
                userChartModel.setDepositCount(null);
            }
            
        }
        staticTodayPointList = staticPoint(staticListToday);
        staticAvgPointList = staticAvgPoint(staticListAvg);
        for (UserTrackDataStatistics userTrackDataStatistics : staticPointList) {
            if (type.equals("0")) {
                visitLists.add(userTrackDataStatistics.getVisitCount());
            } else if (type.equals("1")) {
                visitLists.add(userTrackDataStatistics.getAdvisoryCount());
            } else if (type.equals("2")) {
                visitLists.add(userTrackDataStatistics.getDemoCount());
            } else if (type.equals("3")) {
                visitLists.add(userTrackDataStatistics.getRealCount());
            } else if (type.equals("4")) {
                visitLists.add(userTrackDataStatistics.getDepositCount());
            }
        }
        for (UserTrackDataStatistics userTrackDataStatistics : staticTodayPointList) {
            
            if (type.equals("0")) {
                visitTodayLists.add(userTrackDataStatistics.getVisitCount());
            } else if (type.equals("1")) {
                visitTodayLists.add(userTrackDataStatistics.getAdvisoryCount());
            } else if (type.equals("2")) {
                visitTodayLists.add(userTrackDataStatistics.getDemoCount());
            } else if (type.equals("3")) {
                visitTodayLists.add(userTrackDataStatistics.getRealCount());
            } else if (type.equals("4")) {
                visitTodayLists.add(userTrackDataStatistics.getDepositCount());
            }
        }
        for (UserChartModel serChartModel : staticAvgPointList) {
            
            if (type.equals("0")) {
                visitAvgLists.add(serChartModel.getVisitCount());
            } else if (type.equals("1")) {
                visitAvgLists.add(serChartModel.getAdvisoryCount());
            } else if (type.equals("2")) {
                visitAvgLists.add(serChartModel.getDemoCount());
            } else if (type.equals("3")) {
                visitAvgLists.add(serChartModel.getRealCount());
            } else if (type.equals("4")) {
                visitAvgLists.add(serChartModel.getDepositCount());
            }
        }
        chartModel.setVisitLists(visitLists);
        chartModel.setVisitTodayLists(visitTodayLists);
        chartModel.setVisitAvgLists(visitAvgLists);
        josnResult = JsonUtils.o2j(chartModel);
        return josnResult;
    }
    
    /**
     * 没有达到24个小时，自动补点
     * 
     * @param staticList
     * @return
     */
    public List<UserTrackDataStatistics> staticPoint(List<UserTrackDataStatistics> staticList) {
        List<UserTrackDataStatistics> staticPointList = new ArrayList<UserTrackDataStatistics>();
        UserTrackDataStatistics statisc = null;
        
        for (int i = 0; i <= 23; i++) {
            boolean isContain = false;
            for (int j = 0; j < staticList.size(); j++) {
                
                if (staticList.get(j).getHours() != i) {
                    isContain = false;
                } else {
                    isContain = true;
                    statisc = staticList.get(j);
                    break;
                }
            }
            
            if (isContain) {
                staticPointList.add(statisc);
            } else {
                statisc = new UserTrackDataStatistics();
                statisc.setHours(i);
                statisc.setVisitCount(0);
                statisc.setAdvisoryCount(0);
                statisc.setDemoCount(0);
                statisc.setRealCount(0);
                statisc.setDepositCount(0);
                staticPointList.add(statisc);
                
            }
            
        }
        
        return staticPointList;
    }
    
    /**
     * 平均数
     * 
     * @param staticList
     * @return
     */
    public List<UserChartModel> staticAvgPoint(List<UserChartModel> staticList) {
        List<UserChartModel> staticPointList = new ArrayList<UserChartModel>();
        UserChartModel statisc = null;
        
        for (int i = 0; i <= 23; i++) {
            boolean isContain = false;
            for (int j = 0; j < staticList.size(); j++) {
                if (staticList.get(j).getHours() != i) {
                    isContain = false;
                } else {
                    isContain = true;
                    statisc = staticList.get(j);
                    break;
                }
            }
            
            if (isContain) {
                staticPointList.add(statisc);
            } else {
                statisc = new UserChartModel();
                statisc.setHours(i);
                statisc.setVisitCount(0);
                statisc.setAdvisoryCount(0);
                statisc.setDemoCount(0);
                statisc.setRealCount(0);
                statisc.setDepositCount(0);
                staticPointList.add(statisc);
            }
            
        }
        return staticPointList;
    }
}
